// pages/bangdingyinhangk/bangdingyinhangk.js
var bankCard = require('../../utils/bankCard.js');
const utilbase = require('../../../../utils/setpwdbase64');
const app = getApp();
Page({
  /**
   * 页面的初始数据
   */
  data: {
    yanzhengm: '',
    yzmTime: 60,
    showModal: false, //add弹窗
    mima: '', //密码
    isFocus: false, //控制input 聚焦
    info: ''
  },
  //忘记密码
  towangji() {
    wx.navigateTo({
      url: '/pages/setting/page/shezhi/jiaoyishezhi/jiaoyishezhi',
    })
  },
  // 获取焦点
  getjiaodian() {
    this.setData({
      isFocus: true
    })
  },
  // 填写密码
  setpassword(e) {
    let that = this
    that.setData({
      mima: e.detail.value
    })
    if (e.detail.value.length > 5) {
      wx.request({
        url: app.globalData.myurl,
        data: {
          cmd: "toUntiedAccounts",
          shopId: wx.getStorageSync('shopId'),
          userId: wx.getStorageSync('userId'),
          id: that.data.info.id,
          mobile: that.data.info.mobile,
          tpassword: utilbase.base64encode(utilbase.utf16to8(that.data.mima)),
          smsCode: that.data.yanzhengm
        },
        header: {
          "content-type": "application/x-www-form-urlencoded",
          "token": wx.getStorageSync('token')
        },
        success(res) {
          if (res.data.zjstate == 1) {
            setTimeout(function () {
              wx.hideLoading()
              wx.stopPullDownRefresh() //停止下拉刷新
            }, 0)
            setTimeout(function () {
              wx.showModal({
                title: '您的账号已在其他设备登录，请重新登录！',
                showCancel: false, //是否显示取消按钮false去掉取消按钮
                success: function (res) {
                  if (res.confirm) {
                    wx.clearStorageSync()
                    wx.reLaunch({
                      url: '/pages/login/login',
                    })
                  } else {
                    wx.clearStorageSync()
                    wx.reLaunch({
                      url: '/pages/login/login',
                    })
                  }
                }
              })
            }, 0)
            return
          }
          console.log(res)
          setTimeout(function() {
            wx.hideLoading()
          }, 0)
          setTimeout(function() {
            wx.showToast({
              title: res.data.resultNote,
              icon: 'none',
              duration: 1500
            })
          }, 0)
          if (res.data.result == 0) {
            setTimeout(function() {
              wx.navigateBack({
                delta: 1
              })
            }, 1500)
          }
        }
      });
    }
  },
  // 关闭add弹窗
  close_mask: function() {
    this.setData({
      showModal: false
    })
  },
  // 提交绑定
  tijiao() {
    let that = this
    if (!that.data.yanzhengm) {
      wx.showToast({
        title: '请输入验证码',
        icon: 'none'
      })
      return
    }
    // 显示密码弹窗
    that.setData({
      showModal: true,
      isFocus: true, //控制input 聚焦
      mima: ''
    })

  },
  // 验证码
  getyanzhengm(e) {
    let that = this
    that.setData({
      yanzhengm: e.detail.value
    })
  },

  // 验证码
  huoQu() {
    let that = this
    that.setData({
      yzmTime: 59
    })
    wx.request({
      url: app.globalData.myurl,
      data: {
        cmd: "phoneMSCode",
        userPhone: that.data.info.mobile
      },
      header: {
        "content-type": "application/x-www-form-urlencoded",
        "token": wx.getStorageSync('token')
      },
      success(res) {
        if (res.data.zjstate == 1) {
          setTimeout(function () {
            wx.hideLoading()
            wx.stopPullDownRefresh() //停止下拉刷新
          }, 0)
          setTimeout(function () {
            wx.showModal({
              title: '您的账号已在其他设备登录，请重新登录！',
              showCancel: false, //是否显示取消按钮false去掉取消按钮
              success: function (res) {
                if (res.confirm) {
                  wx.clearStorageSync()
                  wx.reLaunch({
                    url: '/pages/login/login',
                  })
                } else {
                  wx.clearStorageSync()
                  wx.reLaunch({
                    url: '/pages/login/login',
                  })
                }
              }
            })
          }, 0)
          return
        }
        console.log(res)
        setTimeout(function() {
          wx.showToast({
            title: res.data.resultNote,
            icon: 'none'
          })
        }, 0)
      }
    });
    var interval = setInterval(function timestampToTime() {
      var date = that.data.yzmTime
      if (date <= 1) {
        that.setData({
          yzmTime: 60
        })
        clearInterval(interval)
      } else {
        date--
        that.setData({
          yzmTime: date
        })
        console.log(date)
      }
    }, 1000);
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    console.log(options)
    this.setData({
      info: JSON.parse(options.info)
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {
    this.setData({
      showModal: false, //add弹窗
      mima: '', //密码
      isFocus: false, //控制input 聚焦
    })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {
    return {
      title: '众商联采',
      path: "/pages/login/login", // 路径，传递参数到指定页面。
      // imageUrl:'../../../images/logo/LOGO.png'
      imageUrl: app.globalData.logo
    }
  }
})